package com.mdnote.practice.binary_tree;

import com.sun.jmx.snmp.SnmpOidDatabase;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/9/30
 * @description 填充每个节点的下一个右侧节点指针
 */
public class LeetCode116 {

    public Node connect(Node root) {
        if (root == null) {
            return null;
        }
        _connect(root.left, root.right);
        return root;
    }

    private void  _connect(Node node1, Node node2) {
        // Terminator
        if (node1 == null) {
            return;
        }
        // Process current logic
        node1.next = node2;
        // drill down
        _connect(node1.left, node1.right);
        _connect(node2.left, node2.right);
        _connect(node1.right,node2.left);

        // restore current data
    }

    private static class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
           next = _next;
       }
   }

}
